var URL_index = "http://116.77.73.196/wxbapi/?r=Youku/GetSokuAllUser&data={q:'动漫',pg:1}";
var URL_List = "http://116.77.73.196/wxbapi/?r=Youku/GetSokuList&data={q:'动漫',cp:1,od:1,lt:0,limitdate:0}";
var URL_video = 'http://v.youku.com/v_show/id_%3.html?from=s1.8-1-1.2&spm=a2h0k.8191407.0.0';


function removeloading() {
    var load = document.getElementById('loading');
    load.remove();
}


function Enterdeer() {
    window.location = 'http://localhost:3000/index';
}


/**
 * 创建http服务
 * @returns {string}
 */

function createHttp() {
    var xml = '';

    try {
        xml = new ActiveXObject('microsoft.xmlhttp');
    }
    catch (e) {
        try {
            xml = new XMLHttpRequest();
        }
        catch (em) {
            window.alert('Please check your browser');
        }
    }

    return xml;
}


/**
 *
 * js封装ajax
 * @param conf
 */

function createAjax(conf) {
    var type = conf.type;
    var url = conf.url;
    var data = conf.data;
    var dataType = conf.dataType;
    var success = conf.success;
    var err = conf.err;


    if (type == null) {
        type = 'GET';
    }

    if (dataType == null) {
        dataType = 'TEXT';
    }

    var http = createHttp();

    http.open(type, url, true);


    var TYPE = type.toUpperCase();

    if (TYPE == 'GET') {
        http.send(null);
    }

    else if (TYPE == 'POST') {
        http.set('content-type', 'application/x-www-form-urlencoded');
        http.send(data);
    }

    http.onreadystatechange = function () {
        var DATATYPE = dataType.toUpperCase();
        var DATA = data.toUpperCase();

        if (http.readyState == 4 && http.status == 200) {
            if (DATATYPE == 'TEXT') {
                if (success != null) {
                    success(http.responseText)
                }
            }
            else if (DATATYPE == 'JSON') {
                if (success != null) {
                    success(JSON.parse(http.responseText))
                }
            }
            else if (DATATYPE == 'XML') {
                if (success != null) {
                    success(http.responseXML)
                }
            }
        }

    }
}


/**
 * 加载首页数据
 * @constructor
 */

function IndexList() {
    createAjax({
        type: 'GET',
        url: URL_index,
        dataType: 'JSON',
        data: '',
        success: function (DATA) {

            var localurl = window.location.href;
            var jumpurl = localurl + 'list';

            var row = DATA.data;

            var arr = [];
            for (var j in row) {
                arr.push(row[j])
            }

            var html = '';
            for (var i = 0; i < arr.length; i++) {
                var item = arr[i];

                console.log(item)
                html += '<div class="col-lg-6 col-sm-6 col-xs-6">'


                    + '<div class="index-content relative">'
                    + '<a href="' + jumpurl + '">'
                    + '<div class="indexList-pic relative">'
                    + '<img src="' + backImage()() + '"/>'
                    + '</div>'
                    + '<div class="indexList-name relative">'
                    + '<h3 class="indexList-ni relative">' + item.uname + '</h3>'
                    + '</div>'
                    + '</a>'
                    + '</div>'

                    + '</div>'
            }

            document.getElementById('indexList').innerHTML = html;

            removeloading();

        }
    });
}

/**
 * 判断url调取首页数据
 */

function timeout() {
    var thisurl = 'http://localhost:3000/';
    var localurl = window.location.href;

    if (localurl == thisurl) {
        var timer;
        clearTimeout(timer);
        setTimeout(IndexList, 1000 * 3)
    }
}
timeout();


/**
 * 加载列表页数据
 * @constructor
 */
function ListPage() {
    createAjax({
        type: 'GET',
        url: URL_List,
        dataType: 'JSON',
        data: '',
        success: function (DATA) {
            var row = DATA.data;

            var html = '';
            for (var i = 0; i < row.length; i++) {
                var item = row[i];

                var jumpurl = URL_video.replace("%3", item.vid);

                console.log(item)
                html += '<div class="col-lg-6 col-sm-6 col-xs-6">'
                    + '<div class="full-content relative">'
                    + '<a href="' + jumpurl + '">'
                    + '<div class="recomend-pic relative">'

                    + '<img src="' + item.pic + '"/>'
                    + '<i class="iconplay icon icon-control-play"></i>'
                    + '</div>'
                    + '<div class="recomend-name absolute">'
                    + '<div class="back-profy"></div>'
                    + '<h3 class="recomend-ni relative"><i class="icon icon-star"></i>' + item.uname + '</h3>'
                    + '<span class="absolute score">' + item.duration + '</span>'
                    + '</div>'
                    + '</a>'
                    + '</div>'
                    + '</div>'
            }

            document.getElementById('ListPage').innerHTML = html;

            removeloading();

        }
    });
}


function timeoutPage() {
    var thisurl = 'http://localhost:3000/list';
    var localurl = window.location.href;

    if (localurl == thisurl) {
        var timer;
        clearTimeout(timer);
        setTimeout(ListPage, 1000 * 3)
    }
}
timeoutPage();



